package cn.audit.controller.operation;

import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.AuditInstitution;
import cn.audit.bean.pojo.AuditPlan;
import cn.audit.bean.pojo.User;
import cn.audit.service.institution.InstitutionService;
import cn.audit.service.operation.AuditPlanService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author fei人类
 * @date 2019/05/18
 */
@Controller
@RequestMapping("/AuditPlan")
public class AuditPlanController {
    @Resource
    AuditPlanService auditPlanService;

    @Resource
    InstitutionService institutionService;

    @RequestMapping(value = "/findAll")
    public String findAll(Model model){
        List<UserCorrelation> auditPlans=auditPlanService.findAll();
        List<AuditInstitution> ins=institutionService.findAll();
        model.addAttribute("auditplans",auditPlans);
        model.addAttribute("ins",ins);
        return "operation/auditPlan";
    }

    @RequestMapping(value = "/addPlan")
    public String addPlan(Model model,
                          String year,
                          String name,
                          Long type,
                          Long institutions,
                          HttpSession session){
        User user=(User)session.getAttribute("user");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date yeardate=null;
        try {
            yeardate=sdf.parse(year);
        }catch (Exception e){
            System.out.println(e);
        }

        AuditPlan auditPlan=new AuditPlan(yeardate,name,type,(long)1,user.getId(),new Date(),institutions);
        auditPlanService.saveAudit(auditPlan);

        return "redirect:/AuditPlan/findAll";
    }

    @RequestMapping(value = "/submitPlan")
    public String addPlan(Model model,Long id){
        auditPlanService.updateAudit(id);

        return "redirect:/AuditPlan/findAll";
    }

    @RequestMapping(value = "/zzPlan")
    public String zzPlan(Model model,Long id){
        auditPlanService.deleteAudit(id);
        return "redirect:/AuditPlan/findAll";
    }
}
